<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Money_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
        
	}

	public function get_all_recharge_list($start,$limit){
       return $this->db->select('*')->from('moneys')->limit($limit,$start)->order_by('money_id','desc')->get()->result_array();       
    }

  public function get_recharge_list($start,$limit,$user_id='',$order="desc"){
        $query = $this->db->select('*')->from('moneys');
        if($user_id){
          $this->db->where('user_id',$user_id);
        }
        $this->db->where('money_type',3);
        $this->db->limit($limit,$start);
        $this->db->order_by('money_id',$order);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
        
   }
   //查找成功的订单
  public function get_orders_suceses($start,$limit){
        return $this->db->select('*')->from('shop_orders')->where('order_status',2)->limit($limit,$start)->order_by('order_id','desc')->get()->result_array();
   }
  //计算所有成功的订单的总数
  public function count_all_sucess_order($shop_id = ''){
    $this->db->select('order_id');
    $this->db->from('shop_orders');
    if($shop_id){
      $this->db->where('shop_id',$shop_id);
    }
    $this->db->where(array('order_status'=>2));
    $total=$this->db->count_all_results();
    return $total;
  }
  //查找提现列表
  public function get_cash_list($start,$limit,$user_id='',$order){
    $query = $this->db->select('*');
    $this->db->from('cashs');
    if($user_id){
      $this->db->where('user_id',$user_id);
    }
    $this->db->limit($limit,$start);
    $this->db->order_by('cash_id',$order);
    $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
  }
  public function del_moneys_by_id($id){
    $this->db->where('money_id',$id)->delete('moneys');
    return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
  }
  //查询用户的总金额
  public function get_all_money_by_id($id){
    return $this->db->select('user_money')->from('users')->where('user_id',$id)->get()->row_array();

  }
  //添加提申请记录
  public function add_cash($data){
    $this->db->insert('cashs',$data);
      if($this->db->affected_rows() > 0){
        return $this->db->insert_id();
      }else{
        return false;
      }
  }
  //查找一条提现记录
  public function get_cash_by_id($id){
    return $this->db->select('*')->from('cashs')->where('cash_id',$id)->get()->row_array();
  }
  //修改提现记录的状态
  public function save_cash_type($id,$data){
    $this->db->where('cash_id',$id)->update('cashs',$data);
    return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
  }
  //查找用户的金额
  public function get_money_by_user_id($id){
    return $this->db->select('user_money')->from('users')->where('user_id',$id)->get()->row_array();
  }
  //修改用户的余额信息
  public function update_user_moneys_by_id($id,$data){
    $this->db->where('user_id',$id)->update('users',$data);
    return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
  }
  //删除提现记录
  public function del_cash_by_id($id){
    $this->db->where('cash_id',$id)->delete('cashs');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
  }
  //添加数据到money表
  public function add_moneys($data){
    $this->db->insert('moneys',$data);
      if($this->db->affected_rows() > 0){
        return $this->db->insert_id();
      }else{
        return false;
      }
  }
  //计算所有的充值的条数
  public function count_chongzhi($user_id=''){
    $this->db->select('money_id');
    $this->db->from('moneys');
    if($user_id){
      $this->db->where('user_id',$user_id);
    }
    $this->db->where(array('money_type'=>3));
    $total=$this->db->count_all_results();
    return $total;
  }
  //查询某个用户的关键字
  public function get_all_recharge_keyword_list($start,$limit,$user_id,$orders){
      $query = $this->db->select('*');
        $this->db->from('moneys');
        $this->db->where(array('user_id'=>$user_id));
        $this->db->limit($limit,$start);
        $this->db->order_by('money_time',$orders);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //计算某个用户的值
    public function count_all_keyword_money($user_id){
      $this->db->select('money_id');
      $this->db->from('moneys');
      $this->db->where(array('user_id'=>$user_id));
      $total=$this->db->count_all_results();
      return $total;
    }
    //查询某个用户的所有的成功订单
    public function get_orders_suceses_order_by($start,$limit,$shop_id,$orders){
      return $this->db->select('*')->from('shop_orders')->where('order_status',2)->where('shop_id',$shop_id)->limit($limit,$start)->order_by('order_id','desc')->get()->result_array();
    }
    //计算单个用户的提现记录总和
    public function count_cash($user_id){
      $this->db->select('cash_id');
      $this->db->from('cashs');
      $this->db->where(array('user_id'=>$user_id));
      $total=$this->db->count_all_results();
      return $total;
    }
    
}
